package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.RegexUtils;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import static com.leyou.common.constants.MQConstants.ExchangeConstants;
import static com.leyou.common.constants.MQConstants.RoutingKeyConstants;

/**
 * @ClassName : UserServiceImpl  //类名
 * @Description :   //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-21 14:54  //时间
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private static final String KEY_PREFIX = "user";

    private final StringRedisTemplate redisTemplate;
    private final AmqpTemplate amqpTemplate;
    private final PasswordEncoder passwordEncoder;

    public UserServiceImpl(StringRedisTemplate redisTemplate, AmqpTemplate amqpTemplate, PasswordEncoder passwordEncoder) {
        this.redisTemplate = redisTemplate;
        this.amqpTemplate = amqpTemplate;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public Boolean exists(String data, Integer type) {
        /**
         * @Description //TODO 验证用户是否存在
         * @Param [data, type]
         * @return java.lang.Boolean
         */
        if (type!=1 && type!=2){
            throw new LyException(400,"请求参数有误");
        }
        //校验手机或用户是否存在
        return query()
                .eq(type==1,"username",data)
                .eq(type==2,"phone",data)
                .count()==1;
    }

    @Override
    public void sendCode(String phone) {
        /**
         * @Description //TODO 发送短信
         * @Param [phone]
         * @return void
         */
        //1.验证号格式
        if (!RegexUtils.isPhone(phone)) {
            throw new LyException(400,"请求参数有误");
        }
        //2.使用apache的工具类生成6位数字验证码
        String code = RandomStringUtils.randomNumeric(6);
        //3.保存验证码到redis
        redisTemplate.opsForValue().set(KEY_PREFIX + phone,code,5, TimeUnit.MINUTES);
        //4.发送rabbitMQ消息到ly-sms
        HashMap<String, String> msg = new HashMap<>();
        msg.put("phone",phone);
        msg.put("code",code);
        amqpTemplate.convertAndSend(ExchangeConstants.SMS_EXCHANGE_NAME, RoutingKeyConstants.VERIFY_CODE_KEY, msg);
    }

    @Transactional
    @Override
    public void register(User user, String code) {
        /**
         * @Description //TODO 用户注册
         * @Param [user, code]
         * @return void
         */
        //1.校验验证码
        //1.1取出redis中的验证码
        String cacheCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        //1.2比较验证码
        if (!StringUtils.equals(code,cacheCode)) {
            throw new LyException(400,"验证码错误");
        }
        //2.对密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //3.写入数据库
        save(user);
    }

    @Override
    public UserDTO queryUserByPhoneAndPassword(String username, String password) {
        /**
         * @Description //TODO 根据用户名和密码查询用户
         * @Param [username, password]
         * @return com.leyou.user.dto.UserDTO
         */
        //1.根据用户名查询
        User user = getOne(new QueryWrapper<User>().eq("username", username));
        //2.判断是否存在
        if (user==null) {
            //用户名错误
            throw new LyException(400,"用户名或密码错误");
        }
        //3.校验密码
        if (!passwordEncoder.matches(password,user.getPassword())) {
            //密码错误
            throw new LyException(400,"用户名或密码错误");
        }
        //4.转换dto
        return new UserDTO(user);
    }
}
